using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;  
using MSOutlook = Microsoft.Office.Interop.Outlook;
using OfficeCore = Microsoft.Office.Core;


namespace OutlookSharePointExplorer
{

    /// <summary>
    /// A Delegate used to get notified when an Inspector has closed.
    /// </summary>
    /// <param name="ID">the unique ID of the Inspector</param>
    public delegate void OutlookInspectorDelegate(int ID);

    /// <summary>
    /// This Class holds a List of Active Inspectors
    /// </summary>
    internal class ActiveInspectors
    {
        #region Private Variables

        /// <summary>
        /// A Collection of Active Inspectors
        /// </summary>
        System.Collections.SortedList _ActiveInspectors = null;

        /// <summary>
        /// The ID of the previous Inspector
        /// </summary>
        int _PreviousInspectorID = 0;
        
        /// <summary>
        /// The ID of the active Inspector
        /// </summary>
        int _ActiveInspectorID = 0;

        #endregion

        #region Public Variables

        /// <summary>
        /// The ID of the active Inspector
        /// </summary>
        public int ActiveInspectorID
        {
            get { return _ActiveInspectorID; }
        }

        /// <summary>
        /// The ID of the previous Inspector
        /// </summary>
        public int PreviousInspectorID
        {
            get { return _PreviousInspectorID; }
        }

        /// <summary>
        /// Returns the active Inspector, or null
        /// </summary>
        public OutlookInspector ActiveInspector
        {
            get
            {
                if (_ActiveInspectors.ContainsKey(_ActiveInspectorID))
                {
                    return (OutlookInspector)_ActiveInspectors[_ActiveInspectorID];
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Returns the previous Inspector, or null
        /// This can be used if we want to create a linked item
        /// by double clicking into an ViewControl of a Form.
        /// </summary>
        public OutlookInspector PreviousInspector
        {
            get
            {
                // check if the Previous Inspector wasn't closed already
                if (_ActiveInspectors.ContainsKey(_PreviousInspectorID))
                {
                    // if not, we can return the Inspector Object
                    return (OutlookInspector)_ActiveInspectors[_PreviousInspectorID];
                }
                else
                {
                    return null;
                }
            }
        }

        #endregion

        #region Construction / Cleanup

        /// <summary>
        /// Constructor
        /// We create our emty list here
        /// </summary>
        public ActiveInspectors()
        {
            // Create the empty list
            // We start with a initial value for 25 Inspectors, just enough for a Poweruser, Ha Ha Ha
            _ActiveInspectors = new System.Collections.SortedList(25);
        }

        /// <summary>
        /// Destructor
        /// </summary>
        ~ActiveInspectors()
        {
            try
            {
                _ActiveInspectors.Clear();
                _ActiveInspectors = null;
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine (ex.Message);
            }
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Adds a new Inspector to our Collection
        /// </summary>
        /// <param name="myInspector">The explorer to add to our collection</param>
        public void Add(OutlookInspector myInspector)
        {
            try
            {
                // Register for Close Event
                myInspector.Inspector_Closed += new OutlookInspectorDelegate(_Inspector_Inspector_Closed);
                myInspector.Inspector_Activated += new OutlookInspectorDelegate(_Inspector_Inspector_Activated);
                myInspector.Inspector_Deactivated += new OutlookInspectorDelegate(_Inspector_Inspector_Deactivated); 

                // Remember me
                _ActiveInspectors.Add(myInspector.ID, myInspector);

            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        #endregion

        #region Eventhandlers

        /// <summary>
        /// Eventhandler when an Inspector was closed.
        /// We release the Inspector from memory.
        /// </summary>
        /// <param name="ID">The unique ID of the closed Inspector</param>
         void _Inspector_Inspector_Closed(int ID)
        {
            _ActiveInspectors.Remove(ID);
        }

        /// <summary>
        /// Eventhandler when Inspector was deactivated.
        /// We can track the last deactivated Inspector here.
        /// </summary>
        /// <param name="ID">The unique ID of the deactivated Inspector</param>
        void _Inspector_Inspector_Deactivated(int ID)
        {
            _PreviousInspectorID = ID;
        }

        /// <summary>
        /// Eventhandler when an Inspector was activated.
        /// We can track the activated Inspector here.
        /// </summary>
        /// <param name="ID">The unique ID of the activated Inspector</param>
        void _Inspector_Inspector_Activated(int ID)
        {
            _ActiveInspectorID = ID;
        }

        #endregion
    }
}
